package day_2022_9_to_12.Day;

import java.util.HashSet;
import java.util.Set;

/**
 * @author haomin
 * @date 2022/10/10 17:28
 **/
public class Day38_2_Num202_IsHappy {
    class Solution {
        public boolean isHappy(int n) {
            Set<Integer> set = new HashSet<>();
            while(n != 1 && !set.contains(n)){
                set.add(n);
                n = getNext(n);
            }
            return n == 1;
        }

        private int getNext(int n){
            int ans = 0;
            while(n > 0){
                ans += (n % 10) * (n % 10);
                n /= 10;
            }
            return ans;
        }
    }
}